package cn.edu.szpt.qqdemov3.dbutils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;
import androidx.dynamicanimation.animation.SpringAnimation;

import cn.edu.szpt.qqdemov3.LoginActivity;
import cn.edu.szpt.qqdemov3.R;

public class MyDbHelper extends SQLiteOpenHelper {


    public MyDbHelper(@Nullable Context context) {
        super(context, Db_Params.DB_NAME, null, Db_Params.DB_VER);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE [QQ_Login](" +
                "  [qq_num] VARCHAR PRIMARY KEY NOT NULL, " +
                "  [qq_pwd] VARCHAR NOT NULL, " +
                "  [qq_name] VARCHAR NOT NULL, " +
                "  [qq_imgurl] VARCHAR, " +
                "  [qq_online] VARCHAR, " +
                "  [qq_action] VARCHAR, " +
                "  [belong_country] VARCHAR NOT NULL)";
        db.execSQL(sql);

        sql = "CREATE TABLE [QQ_Contact](" +
                "  [contactId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
                "  [qq_num] VARCHAR NOT NULL, " +
                "  [belong_qq] VARCHAR NOT NULL)";
        db.execSQL(sql);

        sql = "CREATE VIEW [view_Contact]" +
                " AS" +
                " SELECT " +
                "       [u].[contactId], " +
                "       [u].[belong_qq], " +
                "       [v].*" +
                " FROM   [QQ_Contact] [u]" +
                "       LEFT JOIN [QQ_Login] [v] ON [u].[qq_num] = [v].[qq_num]";
        db.execSQL(sql);


        initData(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    private void initData(SQLiteDatabase db) {
        String sql = "insert into QQ_Login(qq_num,qq_pwd,qq_name,qq_imgurl,qq_online,qq_action,belong_country) " +
                " values(?,?,?,?,?,?,?)";
        String[][] nums = {{"1001", "1002", "1003", "1004", "1005", "1006"},
                {"2001", "2002", "2003"}, {"3001", "3002", "3003"}};
        String[][] names = {{"刘备", "关羽", "张飞", "赵云", "黄忠", "魏延"},
                {"曹操", "许褚", "张辽"}, {"孙权", "鲁肃", "吕蒙"}};
//        int[][] icons = {{R.drawable.liubei, R.drawable.guanyu, R.drawable.zhangfei, R.drawable.zhaoyun,
//                R.drawable.huangzhong, R.drawable.weiyan},
//                {R.drawable.caocao, R.drawable.xuchu, R.drawable.zhangliao},
//                {R.drawable.sunquan, R.drawable.lusu, R.drawable.lvmeng}};
        String[] countries = {"蜀", "魏", "吴"};

        for (int i = 0; i < nums.length; i++)
            for (int j = 0; j < nums[i].length; j++) {
                db.execSQL(sql, new Object[]{nums[i][j], "123456", names[i][j],
                        LoginActivity.PHOTO_URL + "/" + nums[i][j] + ".jpg",
                        "5G在线", "好好学习", countries[i]});
            }

        sql = "insert into QQ_Contact(qq_num,belong_qq) values(?,?)";
        for (int i = 0; i < nums.length; i++)
            for (int j = 0; j < nums[i].length; j++) {
                if (!nums[i][j].equals("1002"))
                    db.execSQL(sql, new Object[]{nums[i][j], "1002"});
            }
    }
}
